//
//  OrderDetailViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/27.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SwiftyJSON
import SVProgressHUD

class OrderDetailViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    
    var orderID : String?
    var myTableView = UITableView(frame: CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64), style: .Grouped)
    
    
    
    var dataArr = NSMutableArray()
    var infoDic = NSDictionary()
    var deliveryDic = NSDictionary()
    
    
    
    
    let DETAILCELL = "detailCell"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.creatNavView()
        self.getData()
        self.creatTableView()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}


extension OrderDetailViewController{
    
    private func creatNavView(){
        self.view.backgroundColor = UIColor.whiteColor()
        self.automaticallyAdjustsScrollViewInsets = false
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 94/255.0, green: 200/255.0, blue: 252/255.0, alpha: 1)
        
        self.title = "ORDER DETAILS"
        
        let leftBtn = UIButton(type: .Custom)
        leftBtn.setImage(UIImage(named: "nav_back"), forState: .Normal)
        leftBtn.frame = CGRectMake(0, 0, 30, 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), forControlEvents: .TouchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
    }
    
    func leftBtnClick(){
        self.navigationController?.popViewControllerAnimated(true)
    }
}

extension OrderDetailViewController{
    
    private func getData(){
        let parma = ["main_page":"account_history_info","zenid":UserSession.shareInstance.cookie!,"order_id":self.orderID!]
        
        NetworkTool.shareInstance.request(.GET, urlString: APIBASE, parmas: parma) { (result, error) in
            if result != nil{
                let json = JSON(result!)
                print(json)
                
                let dataDic = (result as? NSDictionary)!["data"]as?NSDictionary
                
                self.infoDic = dataDic!["info"]as![String:NSObject]
                self.deliveryDic = dataDic!["delivery"]as![String:NSObject]
                let productArray = dataDic!["products"]as?[[String:NSObject]]
                
                for dic in productArray!{
                    let model = DetailModel(dictionary: dic)
                    self.dataArr.addObject(model)
                }
                
                self.myTableView.reloadData()
            }else{
                SVProgressHUD.showErrorWithStatus("get orderData error:" + String(error))
                print("get orderData error:" + String(error))
            }
        }
        
    }
    
}

extension OrderDetailViewController{
    
    private func creatTableView(){
        
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        let nib = UINib(nibName: "OrderDetailTableViewCell", bundle: nil)
        
        myTableView.registerNib(nib, forCellReuseIdentifier: DETAILCELL)
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return self.dataArr.count
        }else{
            return 0
        }
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(DETAILCELL)
        
        let model = self.dataArr[indexPath.row]as?DetailModel
        (cell as? OrderDetailTableViewCell)?.model = model
        
        return cell!
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return SCREEN_HEIGHT*0.1
        }else if section == 1{
            return SCREEN_HEIGHT * 0.1
        }else{
            return SCREEN_HEIGHT * 0.12
        }
    }
    
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return SCREEN_HEIGHT * 0.2
    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.whiteColor()
        
        if section == 0 {
            
            headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.05)
            let label = UILabel()
            label.frame = CGRectMake(20, 5, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.05-10)
            label.text = "Order Number:" + self.orderID!
            label.font = UIFont.systemFontOfSize(15)
            headerView.addSubview(label)
            
            
            let timeLabel = UILabel()
            timeLabel.frame = CGRectMake(20, 5 + SCREEN_HEIGHT*0.05, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.05-10)
            let a = String(infoDic["date_purchased"])
            
            timeLabel.text = "Order Date:" + a
            timeLabel.font = UIFont.systemFontOfSize(15)
            headerView.addSubview(timeLabel)
            
            
        }else if section == 1{
            
            headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.1)
            
            let label = UILabel()
            label.frame = CGRectMake(20, 5, SCREEN_WIDTH-40, SCREEN_HEIGHT*0.1-10)
            label.text = String(deliveryDic["street_address"]) + "," + String(deliveryDic["city"]) + "," + String(deliveryDic["postcode"]) + "," + String(deliveryDic["state"]) + "," + String(deliveryDic["country"]) + "," + String(deliveryDic["telephone"])
            label.font = UIFont.systemFontOfSize(15)
            label.numberOfLines = 0
            headerView.addSubview(label)
            
        }else{
            headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.12)
            
            let priceLabel = UILabel()
            priceLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH-20, SCREEN_HEIGHT*0.06)
            priceLabel.font = UIFont.systemFontOfSize(15)
            priceLabel.text = "Total Price:" + String(infoDic["total"])
            priceLabel.textAlignment = .Right
            headerView.addSubview(priceLabel)
            
            let statusLabel = UILabel()
            statusLabel.frame = CGRectMake(0, SCREEN_HEIGHT*0.06, SCREEN_WIDTH-20, SCREEN_HEIGHT*0.06)
            statusLabel.font = UIFont.systemFontOfSize(15)
            print(infoDic)
            statusLabel.textAlignment = .Right
            statusLabel.text = "Order Status:" + String(infoDic["orders_status"])
            headerView.addSubview(statusLabel)
        }
        
        return headerView
        
    }
    
    
}













